package 面试题30_包含min函数的栈;

import java.util.Stack;

/**
 * @Author ：xu_xiaofeng.
 * @Date ：Created in 12:21 2021/3/19
 * @Description：
 */
public class MinStack {

    Stack<Integer> mainStack = new Stack<>();
    Stack<Integer> stack = new Stack<>();// 辅助栈，用于维护递增栈
    /** initialize your data structure here. */
    public MinStack() {

    }

    public void push(int x) {
        mainStack.add(x);

        if (stack.size() == 0 || x <= stack.peek()){
            stack.add(x);
        }
    }

    public void pop() {
        int res = mainStack.pop();

        if (stack.peek().equals(res)){
            stack.pop();
        }
    }

    public int top() {
        return mainStack.peek();
    }

    public int min() {
        return stack.peek();
    }
}
